package com.hentie.tilelib;

import com.hentie.mathlib.Vector3;

public class Layer {
	
	//The tiles in the layer
	public Tile[] tiles;
	LayerType layerType;
	
	//The total height and width of the layer
	int layerHeight;
	int layerWidth;



	int totalTile;
	
	public Layer(int layerWidth,int layerHeight, LayerType layerType)
	{
		//The layer position
		this.layerHeight = layerHeight;
		this.layerWidth = layerWidth;
		totalTile = layerWidth*layerHeight;
		
		
		tiles = new Tile[layerWidth*layerHeight];
		this.layerType = layerType;
		//Initialise the tiles
		for(int i = 0; i < layerWidth*layerHeight; i++)
		{
			tiles[i] = new Tile();
			tiles[i].tileID = -1;
		}
	}
	
	public void SetTile(int currentTile, int tileID)
	{
		if(currentTile < totalTile)
		{
			tiles[currentTile].tileID = tileID;
			//Calculat the width and height position
			
			
			tiles[currentTile].gridX = currentTile%layerWidth;
			tiles[currentTile].gridY = currentTile/layerWidth;
		}
		
		
	}
	
	public int GetTileId(int tileNumber)
	{
		if(tileNumber < totalTile)
			return tiles[tileNumber].tileID;
		else
			return -1;
	}
	
	public int GetValidTileCount()
	{
		int valid = 0;
		for(int i = 0; i < totalTile;i++)
		{
			if(tiles[i].tileID != -1)
				valid++;
		}
		return valid;
	}
	
	public int GetTileX(int tileNumber)
	{
		return tileNumber%layerWidth;
	}
	
	public int GetTileY(int tileNumber)
	{
		return tileNumber/layerHeight;
	}
	
	public int[] GetTileList()
	{
		int totalTiles = GetValidTileCount();
		int[] tileList = new int[totalTiles];
		//Go through and add the valid tile numbers
		int currentValid=0;
		for(int i = 0; i <totalTile; i++)
		{
			if(tiles[i].tileID != -1)
			{
				tileList[currentValid]= i;
				currentValid++;
			}
		}
		return tileList;
	}
	
	public int GetRealTileX(int tileNumber)
	{
		if(tileNumber < totalTile)
		return GetTileX(tileNumber)*tiles[tileNumber].tileWidth;
		else
			return -1;
		
	}
	public int GetRealTileY(int tileNumber)
	{
		if(tileNumber < totalTile)
		return GetTileY(tileNumber)*tiles[tileNumber].tileHeight;
		else
			return -1;
	}
	
	public Vector3 GetPositionVector(int tileNumber,float z)
	{
		return new Vector3(GetRealTileX(tileNumber),-GetRealTileY(tileNumber),z);
		
	}

}
